import MysInfo from "../../genshin/model/mys/mysInfo.js"
import Device from "../../device-plugin/model/Device.js"
import  MysFpDB  from "../model/MysFpDB.js"
let device = new Device()


export class Handler extends plugin {
  constructor () {
    super({
      name: '绑定设备',
      priority: -Infinity,
      namespace: '绑定设备',
      rule: [
        {
          reg: "^#(原神|星铁|绝区零)?绑定设备([\\s\\S]*)$",
          fnc: "sendJson",
        },
        {
          reg: "#?绑定帮助",
          fnc: "shebei",
        },
        {
          reg: "#?绑定用户",
          fnc: "user",
        },
      ]
    })
  }

  async sendJson (e) {
    let message = this.e.msg.replace(/.*?(\{.*\})?$/, '$1') || '';
    let uid = await MysInfo.getUid(e, false)
    if (!uid) return false
    let ck = await MysInfo.checkUidBing(uid, e)
    if (!ck) {
      e.reply(['请先绑定ck再使用绑定设备~'], true)
      return true
    }
    if(!message) {
      this.setContext('sendJson')
      e.reply(['请发送设备信息(建议私聊发送)，或者发送“取消”取消绑定'], true)
      return true
    }
    if (message.includes('取消')) {
      e.reply('已取消绑定')
      this.finish('sendJson')
      return true
    }
    await device.bindDevice(e, message, ck)
    this.finish('sendJson')
    return true
  }

  async shebei(e) {
    const msg = [{
      nickname: this.e.sender.card || this.e.user_id,
      user_id: this.e.user_id,
      message: [
        '绑定设备帮助\n',
        '方法一（仅适用于安卓设备）：\n',
        'https://gitee.com/HanaHimeUnica/copy-device-info/raw/master/%E8%AE%BE%E5%A4%87%E4%BF%A1%E6%81%AF.apk\n',
        '1. 使用常用米游社手机下载以上APK，并安装\n',
        '2. 打开后点击按钮复制\n',
        '3. 注！！！！：得到的设备信息最后面oaid后面不是error才行\n',
        segment.image('./plugins/device-plugin/resources/img/id.png'),
        '4. 给机器人发送"#绑定设备"指令\n',
        '5. 机器人会提示发送设备信息\n',
        '6. 粘贴设备信息发送\n',
        '7. 提示绑定成功'
      ]
    },{
      nickname: this.e.sender.card || this.e.user_id,
      user_id: this.e.user_id,
      message: [
        '方法二（推荐使用这个方法，有能力的使用）：\n',
        '1. 使用抓包软件,抓取米游社APP的请求\n',
        '2. 在请求头内找到【x-rpc-device_id】和【x-rpc-device_fp】\n',
        '3. 自行构造如下格式的信息：{"device_id": "x-rpc-device_id的内容", "device_fp": "x-rpc-device_fp的内容"}\n',
        '4. 给机器人发送"#绑定设备"指令\n',
        '5. 机器人会提示发送设备信息\n',
        '6. 粘贴设备信息发送\n',
        '7. 提示绑定成功'
      ]
    },{
      nickname: this.e.sender.card || this.e.user_id,
      user_id: this.e.user_id,
      message: [
        '方法三（使用网页版米游社）：\n',
        '1. 使用米游社大地图,抓取getFp, “必须是经常用浏览器米游社大地图才行”\n',
        'https://act.mihoyo.com/ys/app/interactive-map/index.html\n',
        '2. 找到getFp中body参数中【device_id】和【device_fp】\n',
        segment.image('./plugins/device-plugin/resources/img/id3.png'),
        '3. 自行构造如下格式的信息：{"device_id": "device_id的内容", "device_fp": "device_fp的内容"}\n',
        '4. 给机器人发送"#绑定设备"指令\n',
        '5. 机器人会提示发送设备信息\n',
        '6. 粘贴设备信息发送\n',
        '7. 提示绑定成功'
      ]
    }]
    return await e.reply(Bot.makeForwardMsg(msg))
  }

  async user(e) {

    let user = await MysFpDB.findAllUsers()
    const ltuid = user.map(i => `ltuid：${i.dataValues.ltuid}\n`);

    return await e.reply(Bot.makeForwardMsg([{
      nickname: this.e.sender.card || this.e.user_id,
      user_id: this.e.user_id,
      message: `绑定设备用户数量: ${user.length}`
    },
    {
      nickname: this.e.sender.card || this.e.user_id,
      user_id: this.e.user_id,
      message: ltuid
    }]))
  }
}





